package com.gonsin.gnews;

import com.gonsin.gnews.annotations.model.GNewsContext;
import com.gonsin.gnews.annotations.services.IConfigService;
import com.gonsin.gnews.annotations.services.IConfigValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class NewsContextManager {

    @Autowired private IConfigService configService;
    @Autowired private IConfigValueService configValueService;

    private Map<String, GNewsContext> contextMap = new ConcurrentHashMap<>();

    /**
     * 根据多租户返回每个租户的环境变量
     */
    public GNewsContext getContext(String edificeKey, String platform){
        return contextMap.computeIfAbsent(edificeKey, k -> {
            return new GNewsContext(edificeKey, platform, configValueService, configService);
        });
    }

}
